﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.Configuration;
using System.Web.UI.WebControls;
using System.Security.Principal;
using BusinessLayer;
using System.Security.Permissions;

namespace RoleBasedSecurity
{
    public partial class Login1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (Members.ValidUser(txtUsername.Text, txtPassword.Text))
                {
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, txtUsername.Text, DateTime.Now, DateTime.Now.AddMinutes(10), false, String.Join("|", BusinessLayer.Roles.GetRoles()));
                    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
                    Response.Cookies.Add(cookie);
                    string redirectUrl = FormsAuthentication.GetRedirectUrl(txtUsername.Text, false);
                    Response.Redirect(redirectUrl);
                }
            }
            catch (Exception ex)
            {
            }
        }

    }
}